package org.example.maven.museumuser.service.impl;

import org.example.maven.museumuser.domain.entity.IdentityName;
import org.example.maven.museumuser.mapper.ExhibitionMapper;
import org.example.maven.museumuser.domain.pojo.exhibition.UserTicketOrder;
import org.example.maven.museumuser.service.ExhibitionOrderService;
import org.example.maven.museumuser.service.IdentityNameService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class ExhibitionOrderServiceImpl implements ExhibitionOrderService {
    @Autowired
    private ExhibitionMapper exhibitionMapper;
    @Autowired
    private IdentityNameService identityNameService;

    @Override
    @Transactional
    public String executeOrderTransaction(UserTicketOrder userTicketOrder){
        if (exhibitionMapper.updateExhibitionTicket(userTicketOrder.getTicketId()) > 0) {
            try {
                identityNameService.addIdentityByName(new IdentityName(userTicketOrder.getIdentity(),userTicketOrder.getName()));
                exhibitionMapper.submitExhibitionReservation(userTicketOrder);
                return "预购成功";
            } catch (DuplicateKeyException e) {
                // 数据库唯一约束兜底
                return "该用户已购买";
            }
        } else {
            return "该票已售完";
        }
    }
}
